package com.mayi.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mayi.pojo.User;
import com.mayi.vo.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * @Description: Json 工具类
 * @Auther: yacong_liu
 * @Date: 2019/7/13
 * @version: 1.0
 */
public class JsonUtils {

    public static void SUCCESS(HttpServletResponse response) throws Exception {

        writeJson(response, convertObjectToJson(Result.success()));
    }

    public static void SUCCESS(HttpServletResponse response, Object object) throws Exception {
        if (object == null) {
            return;
        }
        writeJson(response, convertObjectToJson(Result.success().add("data", object)));
    }

    public static void ERROR(HttpServletResponse response, String msg) throws Exception {
        if (msg == null || "".equals(msg.trim())) {
            return;
        }
        writeJson(response, convertObjectToJson(Result.error(msg)));
    }

    public static void ERROR(HttpServletResponse response) throws Exception {

        writeJson(response, convertObjectToJson(Result.error()));
    }


    /**
     * 功能描述: 响应json<br>
     *
     * @Param:
     * @Return:
     * @Author: yacong_liu
     * @Date: 2019/7/14 11:26
     */
    public static void writeJson(HttpServletResponse response, Object object) throws Exception {
        if (null == object) {
            return;
        }
        writeJson(response, convertObjectToJson(object));
    }

    /**
     * 功能描述: 响应JSON<br>
     *
     * @Param: HttpServletResponse response,String json json字符串
     * @Return: void
     * @Author: yacong_liu
     * @Date: 2019/7/14 11:24
     */
    public static void writeJson(HttpServletResponse response, String json) throws Exception {

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter pw;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            throw new Exception("can't open the output stream!");
        }

        pw.write(json);
        pw.flush();
    }

    public static String convertObjectToJson(Object object) {
        if ((object == null) || (object.equals(""))) {
            return "\"\"";
        }

        String json = JSON.toJSONString(object);

        if (null == json) {
            return "\"\"";
        }
        return json;
    }

    /**
     * 功能描述: 支持从RequestPayload中获取Json<br>
     *
     * @Param:
     * @Return:
     * @Author: yacong_liu
     * @Date: 2019/7/14 12:13
     */
    public static <T> T requestPayloadtoJavaObject(HttpServletRequest request, Class<T> clazz) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        JSONObject jsonObj = (JSONObject) JSON.parse(getRequestPayload(request));
        return JSONObject.toJavaObject(jsonObj, clazz);
    }

    /**
     * 功能描述: 将RequestPayload中的内容转成String<br>
     *
     * @Param:
     * @Return:
     * @Author: yacong_liu
     * @Date: 2019/7/14 12:15
     */
    public static String getRequestPayload(HttpServletRequest req) {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader reader = req.getReader();) {
            char[] buff = new char[1024];
            int len;
            while ((len = reader.read(buff)) != -1) {
                sb.append(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
